#include "clockmanager.h"
#include "qterminal.h"
#include "_def_p.h"

bool ClockManager::setDateTime(const QString &datetime, QString *message)
{
    bool ret = true;
    QTerminal terminal(TOKEN);
#ifdef _VMWARE
    terminal.runAsAdmin(QString("systemctl stop vmware-tools.service"));
#endif
    ret = ret && terminal.runAsAdmin(QString("timedatectl set-ntp false"), message);
    ret = ret && terminal.runAsAdmin(QString("systemctl stop systemd-timesyncd.service"), message);
    ret = ret && terminal.runAsAdmin(QString("timedatectl set-timezone Asia/Shanghai"), message);
    ret = ret && terminal.runAsAdmin(QString("date -s \"%1\"").arg(datetime), message);
    ret = ret && terminal.runAsAdmin(QString("hwclock -w"), message);
    return ret;
}

bool ClockManager::setDateTime(const QDateTime &datetime, QString *message)
{
    return setDateTime(datetime.toString("yyyy-MM-dd hh:mm:ss"), message);
}

QString ClockManager::currentDateTime()
{
    QString datetime;
    QTerminal terminal;
    terminal.run(QString("date +'%Y-%m-%d %H:%M:%S'"), &datetime);
    return datetime;
}
